//
//  TestViewController9.m
//  MSDemo
//
//  Created by ms on 2024/7/4.
//

#import "TestViewController9.h"

@interface TestViewController9 ()

@property (nonatomic, strong) UILabel *label;

@end

@implementation TestViewController9

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"滑动解锁动画";
    
    //创建渐变图层
    CAGradientLayer *gradientLayer = [CAGradientLayer layer];
    [self.view.layer addSublayer:gradientLayer];
    gradientLayer.frame = CGRectMake(0, 200, MS_SCREEN_WIDTH, 64);
    //设置渐变轴为从左往右
    gradientLayer.startPoint = CGPointMake(0, 0.5);
    gradientLayer.endPoint = CGPointMake(1, 0.5);
    //黑白相间的颜色
    gradientLayer.colors = @[(__bridge id)[UIColor blackColor].CGColor,
                             (__bridge id)[UIColor whiteColor].CGColor,
                             (__bridge id)[UIColor blackColor].CGColor,];
    //因为要做动画，所以locations属性可以是任何NSArray对象就可以了
    //甚至你可以这样写: .locations = @[@"干死黄旭东", @"蟑螂加两攻"];
    //下面是规范写法
    gradientLayer.locations = @[@0.25, @0.5, @0.75];

    //添加动画
    //动画KeyPath指定为"locations"，表示要对gradientLayer.locations属性做动画
    CABasicAnimation *basicAnimation = [CABasicAnimation animationWithKeyPath:@"locations"];
    basicAnimation.fromValue = @[@0, @0, @0.25];
    basicAnimation.toValue = @[@0.75, @1, @1];
    basicAnimation.duration = 2.5;
    basicAnimation.repeatCount = HUGE;
    [gradientLayer addAnimation:basicAnimation forKey:nil];

    //接下来创建作为蒙板的Label
    //注意这个frame，是相对gradientLayer的坐标
    UILabel *label = [[UILabel alloc] initWithFrame:gradientLayer.bounds];
    label.text = @"滑动狗头解锁 >>";
    label.alpha = 0.5;
    label.textAlignment = NSTextAlignmentCenter;
    label.font = [UIFont systemFontOfSize:30];
    //一定要强引用这个Label避免被释放
    self.label = label;
    //重点：设置Label的layer为gradientLayer的蒙板，不需要做addSubView操作
    gradientLayer.mask = label.layer;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
